<?php
/**
 * Hoffman Framework
 *
 * LICENSE
 *
 * This source file is subject to the GNU LGPL 2.1 license that is bundled
 * with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to annop@thaicyberpoint.com so we can send you a copy immediately.
 *
 * @package	   Hoffman
 * @author     Ford AntiTrust
 * @copyright  Copyright (c) 2008 Ford AntiTrust (http://www.thaicyberpoint.com/ford/blog)
 * @since	   Version Rv 0.2
 * @license    http://creativecommons.org/licenses/LGPL/2.1
 *             GNU LGPL 2.1
 * @filesource
 */

if (!defined('INDEX'))
    die('No direct script access allowed.');

/**
 * @see Zend_Registry
 */
require_once 'Zend/Registry.php';

/**
 * @see Zend_Cache
 */
require_once 'Zend/Cache.php';

/**
 * @see Hmf_Locale
 */
require_once 'Hmf/Locale.php';

/**
 * Config
 *
 * Prase the configuration variable in your specified profile to new structure array data.
 *
 * @package     Hoffman
 * @subpackage  Hmf
 * @category    Config
 * @author      Ford AntiTrust
 * @version     SVN: $Id: Config.php 167 2010-02-18 10:32:16Z annop.s@gmail.com $
 * @link        http://www.thaicyberpoint.com/ford/blog/hmf
 */

class Hmf_Config {

    private static $appConfig;

    public static function getConfig($key = null) {

        if (!is_null($key))
            return self::$appConfig[$key];

        return self::$appConfig;
    }

    public static function setConfig($key, $val) {

        self::$appConfig[$key] = $val;
    }

    public static function process($config) {

        $cacheFrontends = array(
            'lifetime' =>  3600,
            'automatic_serialization' => true
        );
        $cacheBackends = array(
            'cache_dir' => PATH_DIR_TMP_CACHE_APP,
            'file_name_prefix' => 'hmf',
            'hashed_directory_umask' => '0777',
            'cache_file_umask' => '644',
            'hashed_directory_level' => '2'
        );

        Zend_Registry::set('app_cache', Zend_Cache::factory('Core', 'File', $cacheFrontends, $cacheBackends));

        $app_cache = Zend_Registry::get('app_cache');

        if(!self::$appConfig = $app_cache->load('config')) {

            foreach ($config['production'] as $key => $val) {
                self::setConfig($key, $val);
            }
            if (!Zend_Registry::get('is_production')) {
                foreach ($config[ENVIRONMENT] as $outerKey => $outerVal) {
                    if (is_array($outerVal)) {
                        foreach ($outerVal as $innerKey => $innerVal) {
                            self::setConfig($outerKey, array_merge(self::$appConfig[$outerKey], array($innerKey => $innerVal)));
                        }
                    } else {
                        self::setConfig($outerKey, $outerVal);
                    }
                }
            }

            self::setConfig('webhost', (substr(self::getConfig('webhost'), -1) == "/" ? substr(self::getConfig('webhost'), 0, -1) : self::getConfig('webhost')));
            self::setConfig('base_url', (substr(self::getConfig('base_url'), -1) == "/" ? substr(self::getConfig('base_url'), 0, -1) : self::getConfig('base_url')));

            if(Zend_Registry::get('is_production')) {
              $app_cache->save(self::$appConfig, 'config');
            }
        }

        Zend_Registry::set('app_locale', Hmf_Locale::createCoreInstanceLocale((self::getConfig('language.locale'))));
        Zend_Registry::set('app_config', self::getConfig());

        /* Set internal character encoding to UTF-8 */
        mb_internal_encoding(self::getConfig('language.character_set'));

        /* Sets the default timezone used by all date/time functions in a script */
        date_default_timezone_set(self::getConfig('timezone.default'));

    }
}
?>